﻿Imports System
Imports System.Collections.ObjectModel
Imports System.Drawing
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <ActivityDesignerTheme(GetType(WhileDesignerTheme))> _
    Friend NotInheritable Class WhileDesigner
        Inherits SequentialActivityDesigner
        ' Methods
        Public Overrides Function CanInsertActivities(ByVal insertLocation As HitTestInfo, ByVal activitiesToInsert As ReadOnlyCollection([Of] Activity)) As Boolean
            If ((Me Is MyBase.ActiveView.AssociatedDesigner) AndAlso (Me.ContainedDesigners.Count > 0)) Then
                Return False
            End If
            Return MyBase.CanInsertActivities(insertLocation, activitiesToInsert)
        End Function

        Protected Overrides Function GetConnectors() As Rectangle()
            Dim connectors As Rectangle() = MyBase.GetConnectors
            Dim designerTheme As CompositeDesignerTheme = [TryCast](MyBase.DesignerTheme,CompositeDesignerTheme)
            If (Me.Expanded AndAlso (connectors.GetLength(0) > 0)) Then
                connectors((connectors.GetLength(0) - 1)).Height = (connectors((connectors.GetLength(0) - 1)).Height - (If((Not designerTheme Is Nothing), designerTheme.ConnectorSize.Height, 0) / 3))
            End If
            Return connectors
        End Function

        Protected Overrides Sub Initialize(ByVal activity As Activity)
            MyBase.Initialize(activity)
            Me.HelpText = DR.GetString("DropActivityHere")
        End Sub

        Protected Overrides Function OnLayoutSize(ByVal e As ActivityDesignerLayoutEventArgs) As Size
            Dim size As Size = MyBase.OnLayoutSize(e)
            Dim designerTheme As CompositeDesignerTheme = [TryCast](e.DesignerTheme,CompositeDesignerTheme)
            If ((Not designerTheme Is Nothing) AndAlso Me.Expanded) Then
                size.Width = (size.Width + (2 * designerTheme.ConnectorSize.Width))
                size.Height = (size.Height + designerTheme.ConnectorSize.Height)
            End If
            Return size
        End Function

        Protected Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            MyBase.OnPaint(e)
            If Me.Expanded Then
                Dim designerTheme As CompositeDesignerTheme = [TryCast](e.DesignerTheme,CompositeDesignerTheme)
                If (Not designerTheme Is Nothing) Then
                    Dim bounds As Rectangle = MyBase.Bounds
                    Dim textRectangle As Rectangle = Me.TextRectangle
                    Dim imageRectangle As Rectangle = Me.ImageRectangle
                    Dim empty As Point = Point.Empty
                    If Not imageRectangle.IsEmpty Then
                        empty = New Point((imageRectangle.Right + (e.AmbientTheme.Margin.Width / 2)), (imageRectangle.Top + (imageRectangle.Height / 2)))
                    ElseIf Not textRectangle.IsEmpty Then
                        empty = New Point((textRectangle.Right + (e.AmbientTheme.Margin.Width / 2)), (textRectangle.Top + (textRectangle.Height / 2)))
                    Else
                        empty = New Point(((bounds.Left + (bounds.Width / 2)) + (e.AmbientTheme.Margin.Width / 2)), (bounds.Top + (e.AmbientTheme.Margin.Height / 2)))
                    End If
                    Dim points As Point() = New Point(4  - 1) {}
                    points(0).X = (bounds.Left + (bounds.Width / 2))
                    points(0).Y = (bounds.Bottom - (designerTheme.ConnectorSize.Height / 3))
                    points(1).X = (bounds.Right - (designerTheme.ConnectorSize.Width / 3))
                    points(1).Y = (bounds.Bottom - (designerTheme.ConnectorSize.Height / 3))
                    points(2).X = (bounds.Right - (designerTheme.ConnectorSize.Width / 3))
                    points(2).Y = empty.Y
                    points(3).X = empty.X
                    points(3).Y = empty.Y
                    MyBase.DrawConnectors(e.Graphics, designerTheme.ForegroundPen, points, LineAnchor.None, LineAnchor.ArrowAnchor)
                    Dim pointArray2 As Point() = New Point() { points(0), New Point((bounds.Left + (bounds.Width / 2)), bounds.Bottom) }
                    MyBase.DrawConnectors(e.Graphics, designerTheme.ForegroundPen, pointArray2, LineAnchor.None, LineAnchor.None)
                End If
            End If
        End Sub

    End Class
End Namespace

